import 'dart:typed_data';

import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/config/global_configs.dart';
import 'package:ls_work/res/colors.dart';

import '../../net/response/warning_type_list_data.dart';
import '../../viewmodel/warning/set_warning_info_view_model.dart';

class SetWarningInfoPage extends BaseStatefulMvvmWidget {
  @override
  State<StatefulWidget> createState() => _SettingWarningInfoState();
}

class _SettingWarningInfoState
    extends BaseMvvmState<SetWarningInfoViewModel, SetWarningInfoPage> {
  @override
  Widget buildLoadingContentView() {
    return Row(
      children: [
        ///选择分类
        SizedBox(
          width: 16,
        ),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(
              height: 16,
            ),
            Row(
              children: [
                CommonText('选择报警类型', textSize: 16).setWeight(1),
                Row(
                  children: [
                    Icon(viewModel.selectTypes.length==viewModel.warningTypeList.length? Icons.check_box:Icons.check_box_outline_blank,size: 20,),
                    Text(viewModel.selectTypes.length==viewModel.warningTypeList.length?'取消全选':'全选')
                  ],
                ).onTap(() {
                  setState(() {
                    if(viewModel.selectTypes.length==viewModel.warningTypeList.length){
                      viewModel.selectTypes.clear();
                    }else{
                      viewModel.selectTypes.clear();
                      viewModel.selectTypes.addAll(viewModel.warningTypeList.map((e) => e.type?.toInt()??0).toList());
                    }
                  });
                })
              ],
            ),
            SizedBox(
              height: 16,
            ),
            Wrap(
              spacing: 10,
              runSpacing: 10,
              children: viewModel.warningTypeList
                  .map((e) => Text(
                        e.typeName??"",
                        style: TextStyle(
                            fontSize: 16,
                            color: viewModel.selectTypes.contains(e.type)
                                ? Colors.white
                                : Colors.black),
                      )
                          .addToContainer(
                              decoration: BoxDecoration(
                                  color: viewModel.selectTypes.contains(e.type)
                                      ? Colors.blue
                                      : Colors.white,
                                  borderRadius: 4.borderRadius),
                              padding: EdgeInsets.symmetric(
                                  horizontal: 10, vertical: 4))
                          .onTap(() {
                        setState(() {
                          if (viewModel.selectTypes.contains(e.type)) {
                            viewModel.selectTypes.remove(e.type);
                          } else {
                            viewModel.selectTypes.add(e.type!);
                          }
                        });
                      }))
                  .toList(),
            )
          ],
        ).setWeight(1),
        VerticalDivider(),

        ///选择楼栋楼层
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(
              height: 16,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                CommonText('选择楼栋楼层', textSize: 16).setWeight(1),
                Row(
                  children: [
                    Icon(viewModel.isAllBuildingChecked()?Icons.check_box:Icons.check_box_outline_blank,size: 20,),
                    Text(viewModel.isAllBuildingChecked()?"取消全选":"全选")
                  ],
                ).onTap(() {
                  if(viewModel.isAllBuildingChecked()){
                    viewModel.clearAll();
                  }else{
                    viewModel.chooseAll();
                  }
                }),
                // CheckWithText(title: viewModel.isAllBuildingChecked()?"取消全选":"全选", onChanged: (value){
                //   if(value){
                //     viewModel.chooseAll();
                //   }else{
                //     viewModel.clearAll();
                //   }
                // },defaultSelected: viewModel.isAllBuildingChecked(),)
              ],
            ),
            SizedBox(
              height: 16,
            ),
            ListView.separated(
                    itemBuilder: (context, index) {
                      var building = viewModel.buildingList[index];
                      return Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            children: [
                              CommonText(building.buildingNo).setWeight(1),

                              Row(
                                mainAxisSize: MainAxisSize.min,
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Icon(
                                    viewModel.buildingCheckIcon(building),
                                    size: 20,
                                  ),
                                  Text(viewModel.buildingChecked(building)?'取消全选':'全选')
                                ],
                              ).onTap(() {
                                var findBuilding =  viewModel.findedBuilding(building.id);
                                if((building.floorList??[]).isNotEmpty){
                                  if(findBuilding==null){
                                    viewModel.selectBuilding.add(SosBuildingData(buildingId: building.id));
                                  }
                                  var isAll = (viewModel.findedBuilding(building.id)?.floorIds??=[])?.length==(building.floorList??[]).length;
                                  setState(() {
                                    if(!isAll){
                                      (viewModel.findedBuilding(building.id)?.floorIds??=[])?.clear();
                                      (viewModel.findedBuilding(building.id)?.floorIds??=[])?.addAll((building.floorList??[]).map((e) => e.id).toList());
                                    }else{
                                      viewModel.selectBuilding.removeWhere((element) => element.buildingId==building.id);
                                    }
                                  });
                                }else{
                                  setState(() {
                                    if(findBuilding!=null){
                                      viewModel.selectBuilding.removeWhere((element) => element.buildingId==building.id);
                                    }else{
                                      viewModel.selectBuilding.add(SosBuildingData(buildingId: building.id));
                                    }
                                  });
                                }
                              })
                            ],
                          ),
                          Divider(),
                          Wrap(
                            spacing: 8,
                            runSpacing: 8,
                            children: (building.floorList??[])
                                .map((e) => Text(
                                      (e.floorNo??"").trim(),
                                      style: TextStyle(
                                          fontSize: 16,
                                          color: (viewModel
                                                  .isInSelectBuildingFloor(
                                                      building.id, e.id))
                                              ? Colors.white
                                              : Colors.black),
                                    )
                                        .addToContainer(
                                            decoration: BoxDecoration(
                                                color: viewModel
                                                        .isInSelectBuildingFloor(
                                                            building.id, e.id)
                                                    ? Colors.blue
                                                    : Colors.white,
                                                border: Border.all(
                                                    color: viewModel
                                                            .isInSelectBuildingFloor(
                                                                building.id,
                                                                e.id)
                                                        ? Colors.transparent
                                                        : Colors.grey,
                                                    width: 1),
                                                borderRadius: 4.borderRadius),
                                            padding: EdgeInsets.symmetric(
                                                horizontal: 6, vertical: 4))
                                        .onTap(() {
                                      var findBuilding =
                                          viewModel.findedBuilding(building.id);
                                      if (findBuilding != null) {
                                        setState(() {
                                          if ((findBuilding.floorIds??[])
                                              .contains(e.id)) {
                                            findBuilding.floorIds!.remove(e.id);
                                          } else {
                                            (findBuilding.floorIds??=[]).add(e.id);
                                          }
                                        });
                                      } else {
                                        setState(() {
                                          viewModel.selectBuilding.add(
                                              SosBuildingData(
                                                  buildingId: building.id,
                                                  floorIds: [e.id]));
                                        });
                                      }
                                    }))
                                .toList(),
                          )
                        ],
                      ).addToContainer(
                          padding: EdgeInsets.symmetric(
                              horizontal: 10, vertical: 15),
                          decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: 4.borderRadius));
                    },
                    separatorBuilder: (context, index) {
                      return SizedBox(
                        height: 12,
                      );
                    },
                    itemCount: viewModel.buildingList.length)
                .setWeight(1)
          ],
        ).setWeight(1),
        SizedBox(
          width: 16,
        ),
      ],
    );
  }

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      backgroundColor: Colours.bg_color,
      appBar: AppBar(
        title: Text("报警接收设置"),
        actions: [
          TextButton(onPressed: (){
            viewModel.saveSetting();
          }, child: Text('保存'))
        ],
      ),
      body: loadingContentWidget,
    );
  }

  @override
  SetWarningInfoViewModel createViewModel() {
    return SetWarningInfoViewModel(context);
  }

  @override
  void onRetryClick() {}
}
